//
//  SiterBaseNetManager.m
//  sHome
//
//  Created by tracyhenry on 2022/8/15.
//  Copyright © 2022年 SiterWell. All rights reserved.
//
#import "SiterAPI.h"
#import "ErrorModel.h"
#import "AFNetworking.h"
#import "SiterBaseNetManager.h"
#import "NVLogManager.h"
@implementation SiterBaseNetManager

+ (void)HttpGET:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
    [self GET:path parameters:parameters completionHandler:completionHandler];
}


+ (id)GET:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] GET:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NVLogInfo(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error,nil);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
            }
            else if([code intValue]==403){
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil,error, nil);
            }
            else if([code intValue]==401){
                [[Siter sharedInstance] refreshTokenwithCallback:^(id user, NSError *error) {
                    
                    if(error==nil){
                        [[[Siter sharedInstance] sessionWithDefaultAuthorization] GET:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                            NSNumber *code = responseObject[@"code"];
                            if(code==nil){
                                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                !completionHandler?:completionHandler(nil, error,nil);
                            }else{
                                if([code intValue]==200){
                                    !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
                                }else{
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error,nil);
                                }
                            }
                        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                            !completionHandler?:completionHandler(nil,nil, error);
                        }];
                    }else {
                        if(error.code==400||error.code==401)
                        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                        !completionHandler?:completionHandler(nil,nil, error);
                    }
                }];
            }
            else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error,nil);
            }
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        NVLogInfo(@"errResponse %@", errResponse);
        !completionHandler?:completionHandler(nil, nil,error);
    }];
}

+ (void)HttpGETBYJSON:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
    [self GETBYJSON:path parameters:parameters completionHandler:completionHandler];
}


+ (id)GETBYJSON:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorizationJson] GET:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NVLogInfo(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error,nil);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
            }
            else if([code intValue]==403){
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil,error, nil);
            }
            else if([code intValue]==401){
                    [[Siter sharedInstance] refreshTokenwithCallback:^(id user, NSError *error) {
                        
                        if(error==nil){
                            [[[Siter sharedInstance] sessionWithDefaultAuthorizationJson] GET:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                NSNumber *code = responseObject[@"code"];
                                if(code==nil){
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error,nil);
                                }else{
                                    if([code intValue]==200){
                                        !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
                                    }else{
                                        NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                        !completionHandler?:completionHandler(nil, error,nil);
                                    }
                                }
                            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                !completionHandler?:completionHandler(nil,nil, error);
                            }];
                        }else {
                            if(error.code==400||error.code==401)
                            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                            !completionHandler?:completionHandler(nil,nil, error);
                        }
                    }];
            }
            else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error,nil);
            }
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        NVLogInfo(@"errResponse %@", errResponse);
//        ErrorModel *netErrorModel = [[ErrorModel alloc] initWithString:errResponse error:nil];
//        NSError *error1 = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[netErrorModel.code intValue] userInfo:nil];
        
        !completionHandler?:completionHandler(nil, nil,error);

    }];
}


+ (void)HttpPOST:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
        [self POST:path parameters:parameters completionHandler:completionHandler];
}

+ (id)POST:(NSString *)path parameters:(id)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] POST:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NVLogInfo(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error,nil);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
            }
            else if([code intValue]==403){
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil,error, nil);
            }
            else if([code intValue]==401){
                    [[Siter sharedInstance] refreshTokenwithCallback:^(id user, NSError *error) {
                        if(error==nil){
                            [[[Siter sharedInstance] sessionWithDefaultAuthorization] POST:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                NSNumber *code = responseObject[@"code"];
                                if(code==nil){
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error,nil);
                                }else{
                                    if([code intValue]==200){
                                        !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
                                    }else{
                                        NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                        !completionHandler?:completionHandler(nil, error,nil);
                                    }
                                }
                                                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                        !completionHandler?:completionHandler(nil,nil, error);
                                                    }];
                        }else {
                            if(error.code==400||error.code==401)
                            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                            !completionHandler?:completionHandler(nil,nil, error);
                        }
                    }];
            }
            else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error,nil);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        !completionHandler?:completionHandler(nil, nil,error);
    }];
}


+ (void)HttpPOSTBYJSON:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
        [self POSTBYJSON:path parameters:parameters completionHandler:completionHandler];
}

+ (id)POSTBYJSON:(NSString *)path parameters:(id)parameters completionHandler:(void (^)(id, NSError *, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorizationJson] POST:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NVLogInfo(@"POSTBYJSON success %@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error,nil);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
            }
            else if([code intValue]==403){
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil,error, nil);
            }
            else if([code intValue]==401){
                    [[Siter sharedInstance] refreshTokenwithCallback:^(id user, NSError *error) {
                        
                        if(error==nil){
                            [[[Siter sharedInstance] sessionWithDefaultAuthorizationJson] POST:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                NSNumber *code = responseObject[@"code"];
                                if(code==nil){
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error,nil);
                                }else{
                                    if([code intValue]==200){
                                        !completionHandler?:completionHandler(responseObject[@"data"], nil,nil);
                                    }else{
                                        NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                        !completionHandler?:completionHandler(nil, error,nil);
                                    }
                                }
                                                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                        !completionHandler?:completionHandler(nil,nil, error);
                                                    }];
                        }else {
                            if(error.code==400||error.code==401)
                            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUserLoggedOut object:nil userInfo:nil];
                            !completionHandler?:completionHandler(nil,nil, error);
                        }
                    }];
            }
            else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error,nil);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        NVLogInfo(@"errResponse %@", errResponse);
//        ErrorModel *netErrorModel = [[ErrorModel alloc] initWithString:errResponse error:nil];
//        NSError *error1 = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[netErrorModel.code intValue] userInfo:nil];
        !completionHandler?:completionHandler(nil, nil,error);
    }];
}

@end
